package com.itheima.book.manager.controller;

import com.itheima.book.manager.domain.Book;
import com.itheima.book.manager.service.BookService;

import java.util.Scanner;

public class BookController {
    public void start(){
        while (true) {
            System.out.println("--------itcast图书管理系统--------");
            System.out.println("1：添加图书");
            System.out.println("2：删除图书");
            System.out.println("3：修改图书");
            System.out.println("4：查询图书");
            System.out.println("5：退出系统");
            System.out.println("请输入你的选择");

            Scanner scanner = new Scanner(System.in);
            String next = scanner.next();

            switch (next) {
                case "1":
                    //TODO 添加图书
                    //?老师的代码这个方法下面有一条蓝色横线，点击可跳转至方法定义处
                    inputBookInfo();
                    break;
                case "2":
                    //TODO 删除图书
                    deleteByBookId();
                    break;
                case "3":
                    //TODO 修改图书
                    updateBookByBookId();
                    break;
                case "4":
                    //TODO 查询图书
                    findAll();
                    break;
                case "5":
                    //TODO 退出系统
                    System.out.println("谢谢使用");
                    return;//结束所在的方法
                default:
                    System.out.println("输入有误");
                    break;
            }
        }
    }

    Scanner scanner = new Scanner(System.in);
    private BookService bookService = new BookService();
    public void inputBookInfo(){
        //放在这个位置会每次调用方法都产生一个新的bookService对象，
        //进而产生新的数组，因此不能测出图书Id已经存在
//        BookService bookService = new BookService();
        String bookId;
//        Scanner scanner = new Scanner(System.in);
        while (true) {
            System.out.println("请输入图书Id");
            bookId = scanner.next();

//            boolean flag = bookService.isExistsBookId(bookId);
            boolean flag = bookService.isExistsBookId(bookId);
            if (flag) {
                System.out.println("你输入的图书Id已存在，请重新输入");
            } else {
                break;
            }
        }

        System.out.println("请输入图书名称");
        String bookName = scanner.next();
        System.out.println("请输入图书出版日期");
        String publishDate = scanner.next();
        System.out.println("请输入图书价格");
        String price = scanner.next();

        Book book = new Book();
        book.setBookId(bookId);
        book.setBookName(bookName);
        book.setPublishDate(publishDate);
        book.setPrice(price);

//        BookService bookService = new BookService();
        boolean flag = bookService.addBook(book);
        if (flag){
            System.out.println("添加图书成功");
        } else {
            System.out.println("添加图书失败");
        }
    }

    public void findAll(){
        System.out.println("图书Id\t\t图书名称\t\t图书出版日期\t\t图书价格");
        Book[] books = bookService.findAll();
        for (int i = 0; i < books.length; i++) {
            Book book = books[i];
            if (book != null){
                System.out.println(book.getBookId()      + "\t\t" +
                                   book.getBookName()    + "\t\t" +
                                   book.getPublishDate() + "\t\t" +
                                   book.getPrice());
            }
        }
    }

    public void deleteByBookId(){
        Scanner scanner = new Scanner(System.in);
        while (true) {
            System.out.println("请输入你要删除的图书Id");
            String bookId = scanner.next();
            boolean flag = bookService.deleteBookByBookId(bookId);
            if (flag) {
                System.out.println("删除图书成功");
                break;
            } else {
                System.out.println("你输入的图书Id不存在，请重新输入");
            }
        }
    }

    public void updateBookByBookId(){
        while (true) {
            System.out.println("请输入你要修改的图书Id");
            String bookId = scanner.next();
            boolean existsBookId = bookService.isExistsBookId(bookId);
            if (existsBookId) {
                System.out.println("请输入新的图书名称");
                String bookName = scanner.next();
                System.out.println("请输入新的图书出版日期");
                String publishDate = scanner.next();
                System.out.println("请输入新的图书价格");
                String price = scanner.next();

                Book book = new Book();
                book.setBookId(bookId);
                book.setBookName(bookName);
                book.setPublishDate(publishDate);
                book.setPrice(price);

                bookService.updateBookByBookId(book);
                System.out.println("修改图书成功");
                break;
            } else {
                System.out.println("你输入的图书Id不存在，请重新输入");
            }
        }
    }
}
